//二次封装axios


import axios from "axios";
import nProgress from "nprogress";
import 'nprogress/nprogress.css'
import { readToken } from "./auth";



const ajax = axios.create({
  timeout: 30000,
  baseURL:''
})

// 请求拦截器
ajax.interceptors.request.use((config) => {
  
  nProgress.start()
  //读取token
  const token = readToken()
  //有token
  if(token){
    config.headers.token = token
  }
  return config

 
})

// 响应拦截器
ajax.interceptors.response.use(
  (response) => {
    nProgress.done()
    return response.data
  },
   error => {
      // 请求失败返回一个pending状态的promise 中断
     nProgress.done()
     console.log(error)
     return new Promise(()=>{})

  }
)


export default ajax

